package com.common.widget;

import java.util.ArrayList;
import java.util.List;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources.NotFoundException;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.TouchDelegate;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.StarAppConfig;
import com.common.StarAppConst.PRESTIGE_EXP_VIEW;
import com.common.allCommon.CommonUtils;
import com.common.android.ui.widget.LoadingImageView;
import com.common.bean.AppMsgData;
import com.common.bean.FansGroupAuditData;
import com.common.bean.NourishData;
import com.common.filter.LengthFilterWithEllipsis;
import com.common.utils.StarAppUtils;
import com.product.android.business.ApplicationVariable;
import com.starappallcommon.R;

/**
 * 共通顶部模块
 * 
 * <br>
 * Created 2014-5-5 下午2:40:25
 * 
 * @version
 * @author HuangYK
 * 
 * @see
 */
public class CommonActivityTitle extends RelativeLayout {

	/** 装载标题内容，不包含威望经验下拉框 */
	private RelativeLayout mlayoutTitleContent;

	/** 默认按钮宽高dip */
	public static final int DEFALUT_BTN_W_H_DIMENSION = ApplicationVariable.INSTANCE.applicationContext
			.getResources().getDimensionPixelSize(R.dimen.dip_size_25);

	/** 顶部左侧按钮点击类型 */
	public static final int ON_LEFT_CLICK_CALL_BACK = 0x101;
	/** 顶部右侧按钮点击类型 */
	public static final int ON_RIGHT_CLICK_CALL_BACK = 0x102;

	/** 上下文 */
	private Context mContext;
	/** 根视图 */
	private RelativeLayout rlCommonActivityTitle;
	/** 顶部左侧按钮 */
	private Button mBtnTitleLeft;
	/** 顶部左侧按钮右上角新消息红点提示 */
	private ImageView mIvNewMsgTip;

    /** 顶部右侧按钮右上角新消息红点提示 */
    private ImageView mIvRightNewMsgTip;
	/** 顶部左侧Loading转圈 */
	private LoadingImageView mIvTitleLeftLoading;
	/** 顶部右侧按钮 */
	private Button mBtnTitleRight;
	/** 顶部中间文字 */
	private TextView mTvTitleText;

	/** 威望与经验layout */
	private RelativeLayout mlayoutPrestigeExp;

	/** 威望或经验描述, 不要直接使用，调用getDescripTextView()以获得控件 */
	private TextView mtvPrestigeExpDescrip;

	/** 威望或经验描述右上角文字, 不要直接使用，调用getValueChangedTextView()以获得控件 */
	private TextView mtvValueChanged;

	/** 指示收起下拉框的消息 */
	public static final int MSG_BEGIN_TO_HIDE = 2014;

	/** 两秒后发送消息指示收起下拉框 */
	private final int DELAY_MILI_SECONDS = 2 * 1000;

	private TitleBtnCallback mTitleBtnCallback;

	/** 动画是否已经结束 */
	private boolean mbAnimateOver = true;

	private final long duration = 500L;

	/** 广播发过来的下拉消息数据 */
	private List<AppMsgData> mlistToAnimateData = new ArrayList<AppMsgData>();

	/** 顶部按钮点击事件回调 */
	public interface TitleBtnCallback {
		/**
		 * 顶部按钮点击事件处理 左侧点击类型 ：{@link #ON_LEFT_CLICK_CALL_BACK}； 右侧点击类型：
		 * {@link #ON_RIGHT_CLICK_CALL_BACK}
		 * 
		 * <br>
		 * Created 2014-5-5 下午2:40:34
		 * 
		 * @param clickType
		 *            左侧点击类型 ：{@link #ON_LEFT_CLICK_CALL_BACK}； 右侧点击类型：
		 *            {@link #ON_RIGHT_CLICK_CALL_BACK}
		 * @author : HuangYK
		 */
		void onBtnClick(int clickType);
	}

	/**
	 * 
	 * Creates a new instance of CommonActivityTitle. <br>
	 * Created 2014-5-5 下午2:40:52
	 * 
	 * @param context
	 *            上下文
	 */
	public CommonActivityTitle(Context context) {
		super(context);
		init(context, null, 0);
	}

	/**
	 * 
	 * Creates a new instance of CommonActivityTitle. <br>
	 * Created 2014-5-5 下午2:41:04
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 */
	public CommonActivityTitle(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs, 0);
	}

	/**
	 * 
	 * Creates a new instance of CommonActivityTitle. <br>
	 * Created 2014-5-5 下午2:41:13
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 * @param defStyleAttr
	 *            样式
	 */
	public CommonActivityTitle(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context, attrs, defStyleAttr);
	}

	/**
	 * 初始化
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:41:26
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 * @param defStyle
	 *            样式
	 * @author : HuangYK
	 */
	private void init(Context context, AttributeSet attrs, int defStyle) {
		mContext = context;
		LayoutInflater.from(context).inflate(R.layout.common_activity_title,
				this, true);
		initView();
		// 获取自定义属性集
		final TypedArray attrArray = getContext().obtainStyledAttributes(attrs,
				R.styleable.CommonActivityTitle, defStyle, 0);
		// 根据自定义属性集初始化控件
		initAttributes(attrArray);
		// 回收属性集
		attrArray.recycle();
		// 初始化动作事件
		initEvent();
	}

	/**
	 * 初始化View，注册威望经验下拉框广播
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:41:41
	 * 
	 * @author : HuangYK
	 */
	private void initView() {
		rlCommonActivityTitle = this;
		mBtnTitleLeft = (Button) findViewById(R.id.btnTitleLeft);
		mIvNewMsgTip = (ImageView) findViewById(R.id.ivNewMsgTip);
		mIvTitleLeftLoading = (LoadingImageView) findViewById(R.id.ivTitleLeftLoading);
		mBtnTitleRight = (Button) findViewById(R.id.btnTitleRight);
        mIvRightNewMsgTip = (ImageView) findViewById(R.id.ivRightNewMsgTip);
		mTvTitleText = (TextView) findViewById(R.id.tvTitleText);
		mlayoutTitleContent = (RelativeLayout) findViewById(R.id.layout_title_content);
		final int dp10Value = 10;
		final int dp15Value = 15;
		int padding10 = CommonUtils.dipToPx(mContext, dp10Value);
		int padding15 = CommonUtils.dipToPx(mContext, dp15Value);
		mlayoutTitleContent.setPadding(padding10, 0, padding15, 0);

		// 威望经验下拉框
		mlayoutPrestigeExp = (RelativeLayout) findViewById(R.id.layout_prestige_exp);
	}

	@Override
	protected void onAttachedToWindow() {
		LocalBroadcastManager.getInstance(mContext).registerReceiver(
				mMessageReceiver,
				new IntentFilter(PRESTIGE_EXP_VIEW.PRESTIGE_EXP_VIEW_PROC));
		super.onAttachedToWindow();
	}

	@Override
	protected void onDetachedFromWindow() {
		LocalBroadcastManager.getInstance(mContext).unregisterReceiver(
				mMessageReceiver);
		super.onDetachedFromWindow();
	}

	/**
	 * 根据自定义属性初始化控件
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:42:00
	 * 
	 * @param attrArray
	 *            属性列表
	 * @author : HuangYK
	 */
	@TargetApi(Build.VERSION_CODES.CUPCAKE)
	private void initAttributes(TypedArray attrArray) {
		// ====== start 读取是否显示左侧按钮======//
		boolean showLeftBtn = attrArray.getBoolean(
				R.styleable.CommonActivityTitle_showLeftBtn, false);
		if (showLeftBtn) {
			mBtnTitleLeft.setVisibility(View.VISIBLE);
		} else {
			mBtnTitleLeft.setVisibility(View.GONE);
		}
		// ====== finish 读取是否显示左侧按钮======//

		// ====== start 读取是否显示右侧按钮======//
		boolean showRightBtn = attrArray.getBoolean(
				R.styleable.CommonActivityTitle_showRightBtn, false);
		if (showRightBtn) {
			mBtnTitleRight.setVisibility(View.VISIBLE);
		} else {
			mBtnTitleRight.setVisibility(View.INVISIBLE);
		}
		// ====== finish 读取是否显示右侧按钮======//

		// ====== start 读取title文字内容======//
		String titleText = attrArray
				.getString(R.styleable.CommonActivityTitle_titleText);
		if (titleText == null) {
			titleText = "";
		}
		mTvTitleText.setText(titleText);
		// ====== finish 读取title文字内容======//

		// ====== start 读取title中间Logo信息======//
		int titleTextBgResId = attrArray.getResourceId(
				R.styleable.CommonActivityTitle_titleTextBackground, -1);
		if (titleTextBgResId >= 0) {
			mTvTitleText.setBackgroundResource(titleTextBgResId);
		}

		// ====== finish 读取title中间Logo信息======//

		// ====== start 读取左侧按钮文字======//
		String leftBtnText = attrArray
				.getString(R.styleable.CommonActivityTitle_leftBtnText);
		if (!TextUtils.isEmpty(leftBtnText)) {
			mBtnTitleLeft.setText(leftBtnText);
		}
		// ====== finish 读取左侧按钮文字======//

		// ====== start 读取右侧按钮文字======//
		String rightBtnText = attrArray
				.getString(R.styleable.CommonActivityTitle_rightBtnText);
		if (!TextUtils.isEmpty(rightBtnText)) {
			mBtnTitleRight.setText(rightBtnText);
		}
		// ====== finish 读取右侧按钮文字======//

		// ====== start 读取左侧按钮高宽======//

		int leftBtnHeight;
		if (attrArray.hasValue(R.styleable.CommonActivityTitle_leftBtnHeight)) {
			leftBtnHeight = attrArray.getLayoutDimension(
					R.styleable.CommonActivityTitle_leftBtnHeight,
					DEFALUT_BTN_W_H_DIMENSION);
		} else {
			leftBtnHeight = DEFALUT_BTN_W_H_DIMENSION;
		}

		int leftBtnWidth;
		if (attrArray.hasValue(R.styleable.CommonActivityTitle_leftBtnWidth)) {
			leftBtnWidth = attrArray.getLayoutDimension(
					R.styleable.CommonActivityTitle_leftBtnWidth,
					DEFALUT_BTN_W_H_DIMENSION);
		} else {
			leftBtnWidth = DEFALUT_BTN_W_H_DIMENSION;
		}

		mBtnTitleLeft.getLayoutParams().height = leftBtnHeight;
		mBtnTitleLeft.getLayoutParams().width = leftBtnWidth;
		// ====== finish 读取左侧按钮高宽======//

		// ====== start 读取右侧按钮高宽======//

		int rightBtnHeight;
		if (attrArray.hasValue(R.styleable.CommonActivityTitle_rightBtnHeight)) {
			rightBtnHeight = attrArray.getLayoutDimension(
					R.styleable.CommonActivityTitle_rightBtnHeight,
					DEFALUT_BTN_W_H_DIMENSION);
		} else {
			rightBtnHeight = DEFALUT_BTN_W_H_DIMENSION;
		}

		int rightBtnWidth;
		if (attrArray.hasValue(R.styleable.CommonActivityTitle_rightBtnWidth)) {
			rightBtnWidth = attrArray.getLayoutDimension(
					R.styleable.CommonActivityTitle_rightBtnWidth,
					DEFALUT_BTN_W_H_DIMENSION);
		} else {
			rightBtnWidth = DEFALUT_BTN_W_H_DIMENSION;
		}
		mBtnTitleRight.getLayoutParams().height = rightBtnHeight;
		mBtnTitleRight.getLayoutParams().width = rightBtnWidth;
		// ====== finish 读取右侧按钮高宽======//

		// ====== start 读取左侧按钮背景资源======//
		int leftBtnBgResId = attrArray.getResourceId(
				R.styleable.CommonActivityTitle_leftBtnBackground, -1);
		if (leftBtnBgResId >= 0) {
			mBtnTitleLeft.setBackgroundResource(leftBtnBgResId);
		}
		// ====== finish 读取右侧按钮背景资源======//

		// ====== start 读取右侧按钮背景资源======//
		int rightBtnBgResId = attrArray.getResourceId(
				R.styleable.CommonActivityTitle_rightBtnBackground, -1);
		if (rightBtnBgResId >= 0) {
			mBtnTitleRight.setBackgroundResource(rightBtnBgResId);
		}
		// ====== finish 读取右侧按钮背景资源======//

		// ====== start 右侧按钮文字颜色Id======//
		int rightBtnTextColorId = attrArray.getResourceId(
				R.styleable.CommonActivityTitle_rightBtnTextColor,
				R.color.white);
		try {
			mBtnTitleRight.setTextColor(getResources().getColorStateList(
					rightBtnTextColorId));
		} catch (NotFoundException e) {
			mBtnTitleRight.setTextColor(getResources().getColor(R.color.white));
		}

		// ====== finish 右侧按钮文字颜色Id======//

		// ====== start 左侧按钮文字颜色Id======//
		int leftBtnTextColorId = attrArray.getResourceId(
				R.styleable.CommonActivityTitle_rightBtnTextColor,
				R.color.white);
		try {
			mBtnTitleLeft.setTextColor(getResources().getColorStateList(
					leftBtnTextColorId));
		} catch (NotFoundException e) {
			mBtnTitleLeft.setTextColor(getResources().getColor(R.color.white));
		}

		// ====== finish 左侧按钮文字颜色Id======//

		// ====== start 读取背景资源Id======//
		int titleBgResId = attrArray.getResourceId(
				R.styleable.CommonActivityTitle_titleBackground, -1);
		if (titleBgResId >= 0) {
			rlCommonActivityTitle.setBackgroundResource(titleBgResId);
		}
		// ====== finish 读取背景资源Id======//
	}

	/**
	 * 设置操作事件监听
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:42:32
	 * 
	 * @author : HuangYK
	 */
	private void initEvent() {
		mBtnTitleLeft.setOnClickListener(mOnClickListener);
		mBtnTitleRight.setOnClickListener(mOnClickListener);
		mlayoutPrestigeExp.setOnClickListener(mOnClickListener);
		if (mBtnTitleLeft.getVisibility() == View.VISIBLE) {
			setLeftTouchDelegate(true);
		} else {
			setLeftTouchDelegate(false);
		}
		if (mBtnTitleRight.getVisibility() == View.VISIBLE) {
			setRightTouchDelegate(true);
		} else {
			setRightTouchDelegate(false);
		}
	}

	/**
	 * 设置左边按钮热区
	 * 
	 * <br>
	 * Created 2014-5-29 下午4:21:35
	 * 
	 * @param canTouch
	 *            是否可点
	 * @author : HuangYK
	 */
	private void setLeftTouchDelegate(boolean canTouch) {

		final View llBtnLeftParent = (View) mBtnTitleLeft.getParent();
		if (canTouch) {
			final int offset = 50;
			llBtnLeftParent.post(new Runnable() {
				public void run() {
					final Rect r = new Rect();
					mBtnTitleLeft.getHitRect(r);
					r.top -= offset;
					r.bottom += offset;
					r.left -= offset;
					r.right += offset;

					llBtnLeftParent.setTouchDelegate(new TouchDelegate(r,
							mBtnTitleLeft));
				}
			});
		} else {
			llBtnLeftParent.setTouchDelegate(null);
		}
	}

	/**
	 * 设置右边按钮热区
	 * 
	 * <br>
	 * Created 2014-5-29 下午4:21:20
	 * 
	 * @param canTouch
	 *            是否可点
	 * @author : HuangYK
	 */
	private void setRightTouchDelegate(boolean canTouch) {
		final View rlBtnRightParent = (View) mBtnTitleRight.getParent();
		if (canTouch) {
			final int offset = 50;
			rlBtnRightParent.post(new Runnable() {
				public void run() {
					final Rect r = new Rect();
					mBtnTitleRight.getHitRect(r);
					r.top -= offset;
					r.bottom += offset;
					r.left -= offset;
					r.right += offset;

					rlBtnRightParent.setTouchDelegate(new TouchDelegate(r,
							mBtnTitleRight));
				}
			});
		} else {
			rlBtnRightParent.setTouchDelegate(null);
		}
	}

	/**
	 * 设置顶部Title文字
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:42:47
	 * 
	 * @param titleTextResId
	 *            文字资源id
	 * @author : HuangYK
	 */
	public void setTitleText(int titleTextResId) {
		setTitleText(mContext.getString(titleTextResId));
	}

	/**
	 * 设置顶部Title文字
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:42:55
	 * 
	 * @param titleTextString
	 *            文字内容
	 * @author : HuangYK
	 */
	public void setTitleText(String titleTextString) {
		if (TextUtils.isEmpty(titleTextString)) {
			titleTextString = "";
		}
		initTitleText(titleTextString);
	}

	/**
	 * 对内容长度进行限制
	 * 
	 * <br>
	 * Created 2014-7-7 下午12:02:41
	 * 
	 * @param content
	 *            内容
	 * @author : HuangYK
	 */
	private void initTitleText(String content) {
		int w = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		final View rlBtnRightParent = (View) mBtnTitleRight.getParent();
		rlBtnRightParent.measure(w, h);
		int rightWidth = rlBtnRightParent.getMeasuredWidth();

		final View rlBtnLeftParent = (View) mBtnTitleLeft.getParent();
		rlBtnLeftParent.measure(w, h);
		int leftWidth = rlBtnLeftParent.getMeasuredWidth();

		mIvTitleLeftLoading.measure(w, h);
		int loadingWidht = mIvTitleLeftLoading.getMeasuredWidth();

		int margin = (int) mContext.getResources().getDimension(
				R.dimen.dip_size_10);
		leftWidth += loadingWidht + margin * 2;

		int width = ApplicationVariable.INSTANCE.displayMetrics.widthPixels
				- rightWidth - leftWidth;
		CharSequence charSequence = TextUtils.ellipsize(content,
				mTvTitleText.getPaint(), width, TextUtils.TruncateAt.END);
		mTvTitleText.setText(charSequence);
	}

	/**
	 * 设置顶部Title背景颜色
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:43:04
	 * 
	 * @param colorResId
	 *            颜色资源id
	 * 
	 * @author : HuangYK
	 */
	public void setTitleBackgroundColor(int colorResId) {
		rlCommonActivityTitle.setBackgroundColor(mContext.getResources()
				.getColor(colorResId));
	}

	/**
	 * 设置顶部Title背景
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:43:16
	 * 
	 * @param resid
	 *            资源id
	 * @author : HuangYK
	 */
	public void setTitlebackgourndResource(int resid) {
		rlCommonActivityTitle.setBackgroundResource(resid);
	}

	/**
	 * 设置顶部按钮点击事件回调
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:43:32
	 * 
	 * @param titleBtnCallBack
	 *            顶部按钮点击事件回调
	 * @author : HuangYK
	 */
	public void setTitleBtnCallback(TitleBtnCallback titleBtnCallBack) {
		mTitleBtnCallback = titleBtnCallBack;
	}

	/**
	 * 显示Title右侧按钮
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:43:43
	 * 
	 * @author : HuangYK
	 */
	public void showRightBtn() {
		mBtnTitleRight.setVisibility(View.VISIBLE);
		setRightTouchDelegate(true);
	}

	/**
	 * 隐藏Title右侧按钮
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:43:49
	 * 
	 * @author : HuangYK
	 */
	public void hideRightBtn() {
		mBtnTitleRight.setVisibility(View.GONE);
		setRightTouchDelegate(false);
	}

	/**
	 * 显示Title左侧按钮
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:43:54
	 * 
	 * @author : HuangYK
	 */
	public void showLeftBtn() {
		mBtnTitleLeft.setVisibility(View.VISIBLE);
		setLeftTouchDelegate(true);
	}

	/**
	 * 隐藏Title左侧按钮
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:43:58
	 * 
	 * @author : HuangYK
	 */
	public void hideLeftBtn() {
		mBtnTitleLeft.setVisibility(View.GONE);
		setLeftTouchDelegate(false);
	}

	/**
	 * 显示新消息提示红点
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:44:04
	 * 
	 * @author : HuangYK
	 */
	public void showNewMsgTip() {
		mIvNewMsgTip.setVisibility(View.VISIBLE);
	}

	/**
	 * 隐藏新消息提示红点
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:44:07
	 * 
	 * @author : HuangYK
	 */
	public void hideNewMsgTip() {
		mIvNewMsgTip.setVisibility(View.GONE);
	}

	/**
	 * 显示顶部左侧的loading转圈
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:44:12
	 * 
	 * @author : HuangYK
	 */
	public void showTitleLeftLoading() {
		if (mIvTitleLeftLoading.getVisibility() != View.VISIBLE) {
			mIvTitleLeftLoading.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 隐藏顶部左侧的loading转圈
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:44:16
	 * 
	 * @author : HuangYK
	 */
	public void hideTitleLeftLoading() {
		mIvTitleLeftLoading.setVisibility(View.GONE);
	}

	/**
	 * 设置顶部左侧的loading转圈图标
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:44:20
	 * 
	 * @param resId
	 *            资源id
	 * @author : HuangYK
	 */
	public void setTitleLeftLoadingBg(int resId) {
		mIvTitleLeftLoading.setLoadingIcon(resId);
	}

	/**
	 * 设置顶部右侧按钮的enabled状态
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:44:30
	 * 
	 * @param enabled
	 *            True if this view is enabled, false otherwise.
	 * @author : HuangYK
	 */
	public void setRightBtnEnabel(boolean enabled) {
		mBtnTitleRight.setEnabled(enabled);
		setRightTouchDelegate(enabled);
	}

	/**
	 * 设置顶部左侧按钮的enabled状态
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:44:57
	 * 
	 * @param enabled
	 *            True if this view is enabled, false otherwise.
	 * @author : HuangYK
	 */
	public void setLeftBtnEnabel(boolean enabled) {
		mBtnTitleLeft.setEnabled(enabled);
		setLeftTouchDelegate(enabled);
	}

    /**
     * 显示右边新消息提示红点
     *
     * <br>
     * Created 2014-5-5 下午2:44:04
     *
     * @author : HuangYK
     */
    public void showRightNewMsgTip() {
        mIvRightNewMsgTip.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏新消息提示红点
     *
     * <br>
     * Created 2014-5-5 下午2:44:07
     *
     * @author : HuangYK
     */
    public void hideRightNewMsgTip() {
        mIvRightNewMsgTip.setVisibility(View.GONE);
    }

	/**
	 * 设置右侧按钮背景
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:45:05
	 * 
	 * @param resId
	 *            资源id
	 * @author : HuangYK
	 */
	public void setRightBtnBackground(int resId) {
		mBtnTitleRight.setBackgroundResource(resId);
	}

	/**
	 * 设置左侧按钮背景
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:45:12
	 * 
	 * @param resId
	 *            资源id
	 * @author : HuangYK
	 */
	public void setLeftBtnBackground(int resId) {
		mBtnTitleLeft.setBackgroundResource(resId);
	}

	/**
	 * 设置右侧按钮文字颜色
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:45:18
	 * 
	 * @param colorResId
	 *            颜色资源id
	 * @author : HuangYK
	 */
	public void setRightBtnTextColor(int colorResId) {
		mBtnTitleRight.setTextColor(getResources().getColor(colorResId));
	}

	/**
	 * 设置右侧按钮文字内容
	 * 
	 * <br>
	 * Created 2014-6-11 下午3:17:32
	 * 
	 * @param text
	 *            文字内容
	 * @author : HuangYK
	 */
	public void setRightBtnText(CharSequence text) {
		mBtnTitleRight.setText(text);
	}

	/**
	 * 设置左侧按钮文字内容
	 * 
	 * <br>
	 * Created 2014-6-11 下午3:28:51
	 * 
	 * @param text
	 *            文字内容
	 * @author : HuangYK
	 */
	public void setLeftBtnText(CharSequence text) {
		mBtnTitleLeft.setText(text);
	}

	/**
	 * 设置右侧按钮文字大小
	 * 
	 * <br>
	 * Created 2014-6-12 下午4:01:08
	 * 
	 * @param resId
	 *            文字大小资源id
	 * 
	 * @author : HuangYK
	 */
	public void setRightBtnTextSize(int resId) {
		mBtnTitleRight.setTextSize(StarAppUtils.getDiminesSpValue(mContext,
				resId));
	}

	/**
	 * 涉资右侧按钮高度
	 * 
	 * <br>
	 * Created 2014-6-12 下午5:41:36
	 * 
	 * @param diminesId
	 * @author : HuangYK
	 */
	public void setRightBtnHeight(int height) {
		mBtnTitleRight.getLayoutParams().height = height;
	}

	/**
	 * 设置右侧按钮宽度
	 * 
	 * <br>
	 * Created 2014-6-12 下午5:43:13
	 * 
	 * @param widht
	 * @author : HuangYK
	 */
	public void setRightBtnWidth(int width) {
		mBtnTitleRight.getLayoutParams().width = width;
	}

	/**
	 * 设置标题字体
	 * 
	 * <br>
	 * Created 2014-7-31 上午10:56:40
	 * 
	 * @param typeface
	 *            字体对象
	 * @param isBlod
	 *            是否粗体
	 * @author : HuangYK
	 */
	public void setTitleTextTypeface(Typeface typeface, boolean isBlod) {
        StarAppUtils.setViewTypeFace(mTvTitleText, isBlod);
//		mTvTitleText.setTypeface(typeface);
//		if (isBlod) {
//			mTvTitleText.getPaint().setFakeBoldText(true);
//		}

		// 设置下拉框字体
        StarAppUtils.setViewTypeFace(getDescripTextView(), false);
        StarAppUtils.setViewTypeFace(getValueChangedTextView(), false);
//		getDescripTextView().setTypeface(typeface);
//		getValueChangedTextView().setTypeface(typeface);
	}

	/**
	 * 设置右边字体
	 * 
	 * <br>
	 * Created 2014-8-13 下午7:26:28
	 * 
	 * @param typeface
	 *            字体对象
	 * @param isBlod
	 *            是否粗体
	 * @author linsj
	 */
	public void setRightBtnTextTypeface(Typeface typeface, boolean isBlod) {

        StarAppUtils.setViewTypeFace(mBtnTitleRight, isBlod);
//		mBtnTitleRight.setTypeface(typeface);
//		if (isBlod) {
//			mBtnTitleRight.getPaint().setFakeBoldText(true);
//		}
	}

	/**
	 * 设置左侧按钮字体
	 * 
	 * <br>
	 * Created 2014-7-31 上午10:56:11
	 * 
	 * @param typeface
	 *            字体对象
	 * @param isBlod
	 *            是否粗体
	 * @author : HuangYK
	 */
	public void setLeftBtnTypeface(Typeface typeface, boolean isBlod) {
        StarAppUtils.setViewTypeFace(mBtnTitleLeft, isBlod);
//        mBtnTitleLeft.setTypeface(typeface);
//		if (isBlod) {
//			mBtnTitleLeft.getPaint().setFakeBoldText(true);
//		}
	}

	/**
	 * 设置右侧按钮字体
	 * 
	 * <br>
	 * Created 2014-7-31 上午10:57:13
	 * 
	 * @param typeface
	 *            字体对象
	 * @param isBlod
	 *            是否粗体
	 * @author : HuangYK
	 */
	public void setRightBtnTypeface(Typeface typeface, boolean isBlod) {

        StarAppUtils.setViewTypeFace(mBtnTitleRight, isBlod);
//		mBtnTitleRight.setTypeface(typeface);
//		if (isBlod) {
//			mBtnTitleRight.getPaint().setFakeBoldText(true);
//		}
	}

	/**
	 * 设置标题文字最长字数
	 * 
	 * <br>
	 * Created 2014-9-10 下午4:06:24
	 * 
	 * @param maxLength
	 *            标题文字最长字数
	 * @author : HuangYK
	 */
	public void setTitleMaxLength(int maxLength) {
		mTvTitleText
				.setFilters(new InputFilter[] { new LengthFilterWithEllipsis(maxLength) });
	}

	/**
	 * 点击事件监听
	 */
	private OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int vId = v.getId();
			if (R.id.btnTitleLeft == vId) {
				if (mTitleBtnCallback != null) {
					mTitleBtnCallback.onBtnClick(ON_LEFT_CLICK_CALL_BACK);
				}
			} else if (R.id.btnTitleRight == vId) {
				if (mTitleBtnCallback != null) {
					mTitleBtnCallback.onBtnClick(ON_RIGHT_CLICK_CALL_BACK);
				}
			} else if (R.id.layout_prestige_exp == vId) {
				beginAnimate(false);
			}
		}
	};

	/** 威望经验的广播接收 */
	private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (satisfyToAnimate(context)) {
				doAnimiate(intent);
			}
		}
	};

	/**
	 * 是否满足下拉动画的条件 流程：先比较标题栏所在activity与当前最顶端activity名是否一样，一样就返回true
	 * 如果与顶端不一样，再比较与parent是否一样，当顶端是mainFrameActivity时会一样 <br>
	 * Created 2014-5-21 下午4:45:13
	 * 
	 * @param context
	 *            发送广播的context
	 * @return 是否满足条件
	 * @author : linsj
	 */
	private boolean satisfyToAnimate(Context context) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> runningTasks = am.getRunningTasks(1);
		for (RunningTaskInfo task : runningTasks) {
			String strTopName = task.topActivity.getClassName();
			if (strTopName.equals(mContext.getClass().getName())) {
				// 与顶端一样，返回true
				return true;
			} else {
				if (mContext instanceof Activity) {
					Activity activityParent = ((Activity) mContext).getParent();
					if (null != activityParent) {
						// 如果与parent一样，也满足条件（这个时候mainframe在最前端）
						String strParentName = activityParent.getClass()
								.getName();
						if (strTopName.equalsIgnoreCase(strParentName)) {
							return true;
						}
					}
				}
			}

			break;
		}

		return false;
	}

	/**
	 * 判断是哪一种动画类型并执行动画 <br>
	 * Created 2014-5-21 下午4:49:13
	 * 
	 * @param intent
	 *            发送广播传过来的intent
	 * @author : linsj
	 */
	private void doAnimiate(Intent intent) {
		int iType = intent.getIntExtra(PRESTIGE_EXP_VIEW.DROP_DOWN_TYPE, -1);

		// 往列表中加入待下拉动画的数据
		if (PRESTIGE_EXP_VIEW.TYPE_PRESTIGE_EXP == iType) {
			addNourishMsgData(intent);
		} else if (PRESTIGE_EXP_VIEW.TYPE_FANS_GROUP_AUDIT == iType) {
			addFansGroupAuditMsgData(intent);
		}

		if (mbAnimateOver) {
			beginAnimate(true);
		}
	}

	/**
	 * 显示养成数据
	 * 
	 * <br>
	 * Created 2014-7-10 下午3:53:57
	 * 
	 * @param intent
	 *            intent
	 * @author : linsj
	 */
	private void addNourishMsgData(Intent intent) {
		NourishData data = new NourishData();
		data.parseJson(intent
				.getStringExtra(PRESTIGE_EXP_VIEW.PRESTIGE_EXP_CONTENT));

		if (NourishData.CHNG_TYPE_GRADE_PRESTIGE == data.iChangeType) {
			// 如果是既有经验又有威望，则要有两次下拉，这里直接再来一份
			data.iChangeType = NourishData.CHNG_TYPE_GRADE;
			putToAnimateData(data);

			NourishData dataPrestige = new NourishData();
			dataPrestige.parseJson(intent
					.getStringExtra(PRESTIGE_EXP_VIEW.PRESTIGE_EXP_CONTENT));
			dataPrestige.iChangeType = NourishData.CHNG_TYPE_PRESTIGE;
			putToAnimateData(dataPrestige);
		} else {
			putToAnimateData(data);
		}
	}

	/**
	 * 加入需要下拉展示的应用消息数据
	 * 
	 * <br>
	 * Created 2014-7-28 下午5:48:16
	 * 
	 * @param data
	 *            应用消息
	 * @author : linsj
	 */
	private void putToAnimateData(AppMsgData data) {
		synchronized (mlistToAnimateData) {
			mlistToAnimateData.add(data);
		}
	}

	/**
	 * 显示抱团审核结果
	 * 
	 * <br>
	 * Created 2014-7-10 下午3:53:57
	 * 
	 * @param intent
	 *            intent
	 * @author : linsj
	 */
	private void addFansGroupAuditMsgData(Intent intent) {
		String strJson = intent
				.getStringExtra(PRESTIGE_EXP_VIEW.PRESTIGE_EXP_CONTENT);

		FansGroupAuditData data = new FansGroupAuditData();
		data.parseJson(strJson);

		putToAnimateData(data);
	}

	/**
	 * 显示升级了 <br>
	 * Created 2014-5-8 下午10:04:31
	 * 
	 * @author : linsj
	 */
	private void showGradeChangeText(NourishData data) {
		getDescripTextView().setText(
				String.format(mContext
						.getString(R.string.common_congratulations_on_upgrade),
						data.lNewGrade));
		getValueChangedTextView().setVisibility(View.GONE);
	}

	/**
	 * 显示加威望了 <br>
	 * Created 2014-5-8 下午10:04:31
	 * 
	 * @author : linsj
	 */
	private void showPrestigeChangeText(NourishData data) {
		getDescripTextView().setText(
				String.format(
						mContext.getString(R.string.common_prestige_value),
						data.lNewPrestige));
		getValueChangedTextView().setText(
				"+" + (data.lNewPrestige - data.lOldPrestige));
		getValueChangedTextView().setVisibility(View.VISIBLE);
	}

	/**
	 * 显示下拉框上的文字
	 * 
	 * <br>
	 * Created 2014-7-28 下午6:27:23
	 * 
	 * @return 是否成功修改了文字
	 * @author : linsj
	 */
	private boolean showAnimateText() {
		if (null != mlistToAnimateData && !mlistToAnimateData.isEmpty()) {
			AppMsgData data = mlistToAnimateData.get(0);
			mlistToAnimateData.remove(0);

			if (data instanceof NourishData) {
				// 显示养成数据
				NourishData nrsData = (NourishData) data;
				if (NourishData.CHNG_TYPE_GRADE == nrsData.iChangeType) {
					showGradeChangeText(nrsData);
				} else if (NourishData.CHNG_TYPE_PRESTIGE == nrsData.iChangeType) {
					showPrestigeChangeText(nrsData);
				}

				return true;
			} else if (data instanceof FansGroupAuditData) {
				// 显示抱团审核结果
				FansGroupAuditData fgaData = (FansGroupAuditData) data;
				getDescripTextView().setText(fgaData.strMsg);
				getValueChangedTextView().setVisibility(View.GONE);

				return true;
			}
		}

		return false;
	}

	/**
	 * @n<b>函数名称</b> :beginAnimate
	 * @brief　威望与经验下拉框的动画
	 * @see
	 * @since Ver 1.1
	 * @param bShow
	 *            是否显示
	 * @<b>作者</b> : linsj
	 * @<b>创建时间</b> : 2014-4-28上午11:49:06
	 */
	public void beginAnimate(final boolean bShow) {
		if (bShow && !showAnimateText()) {
			return;
		}
		mbAnimateOver = false;
		mlayoutPrestigeExp.post(new Runnable() {
			@Override
			public void run() {
				TranslateAnimation animation = null;
				if (bShow) {
					mlayoutPrestigeExp.setVisibility(View.VISIBLE);
					animation = new TranslateAnimation(
							Animation.RELATIVE_TO_SELF, 0.0f,
							Animation.RELATIVE_TO_SELF, 0.0f,
							Animation.RELATIVE_TO_SELF, -1.0f,
							Animation.RELATIVE_TO_SELF, 0.0f);
				} else {
					animation = new TranslateAnimation(
							Animation.RELATIVE_TO_SELF, 0.0f,
							Animation.RELATIVE_TO_SELF, 0.0f,
							Animation.RELATIVE_TO_SELF, 0.0f,
							Animation.RELATIVE_TO_SELF, -1.0f);
				}

				animation.setDuration(duration);

				animation
						.setAnimationListener(new Animation.AnimationListener() {
							@Override
							public void onAnimationStart(Animation animation) {
							}

							@Override
							public void onAnimationRepeat(Animation animation) {
							}

							@Override
							public void onAnimationEnd(Animation animation) {
								mlayoutPrestigeExp.clearAnimation();
								mlayoutPrestigeExp.setAnimation(null);

								if (bShow) {
									handler.sendMessageDelayed(handler
											.obtainMessage(MSG_BEGIN_TO_HIDE),
											DELAY_MILI_SECONDS);
								}

								if (!bShow) {
									mlayoutPrestigeExp.setVisibility(View.GONE);
									if (null != mlistToAnimateData) {
										synchronized (mlistToAnimateData) {
											if (!mlistToAnimateData.isEmpty()) {
												beginAnimate(true);
											} else {
												mbAnimateOver = true;
											}
										}
									}
								}
							}
						});

				mlayoutPrestigeExp.startAnimation(animation);
			}
		});
	}

	/** 计时器， */
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case MSG_BEGIN_TO_HIDE:
				beginAnimate(false);
				break;
			default:
				break;
			}
		}
	};

	/**
	 * @n<b>函数名称</b> :getHideLayout
	 * @brief 获得威望经验描述TextView
	 * @see
	 * @since Ver 1.1
	 * @return TextView视图
	 * @<b>作者</b> : linsj
	 * @<b>创建时间</b> : 2014-5-4下午6:02:41
	 */
	private TextView getDescripTextView() {
		if (null == mtvPrestigeExpDescrip) {
			mtvPrestigeExpDescrip = (TextView) findViewById(R.id.tv_prestige_exp_descrip);
		}
		return mtvPrestigeExpDescrip;
	}

	/**
	 * @n<b>函数名称</b> :getValueChangedTextView
	 * @brief 获得右上角变化值TextView
	 * @see
	 * @since Ver 1.1
	 * @return TextView视图
	 * @<b>作者</b> : linsj
	 * @<b>创建时间</b> : 2014-5-4下午6:02:41
	 */
	private TextView getValueChangedTextView() {
		if (null == mtvValueChanged) {
			mtvValueChanged = (TextView) findViewById(R.id.tv_value_changed);
		}
		return mtvValueChanged;
	}

}
